package ch.bfh.swos.learnaid.rest.controller;

import java.util.Collection;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import ch.bfh.swos.learnaid.service.QuestionService;
import ch.bfh.swos.learnaid.service.dto.QuestionDTO;
import ch.bfh.swos.learnaid.service.dto.TagDTO;

@Controller
@RequestMapping("/questions")
public class QuestionController {

	@Inject
	private QuestionService questionService;

	/**
	 * Create
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public QuestionDTO createQuestion(@RequestBody QuestionDTO question) {
	    QuestionDTO createdBook = questionService.create(question);
		System.out.println("Question created with id = " + createdBook.getId());
		return createdBook;
	}

	/**
	 * ReadAll
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Collection<QuestionDTO> getQuestions() {
		System.out.println("Collection of Questions requested");
		return questionService.list();
	}

	/**
	 * Read
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public QuestionDTO getQuestion(@PathVariable long id) {
		System.out.println("Question requested with id = " + id);
		return questionService.read(id);
	}

	/**
	 * Update
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	@ResponseBody
	public QuestionDTO updateQuestion(@RequestBody QuestionDTO question, @PathVariable long id) {
	    System.out.println("Method found: ");
	    QuestionDTO updatedQuestion = questionService.update(question);
		System.out.println("Question updated with id = " + updatedQuestion.getId());
		return updatedQuestion;
	}

	/**
	 * Delete
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.OK)
	public void deleteQuestion(@PathVariable long id) {
		QuestionDTO question = questionService.read(id);
		questionService.delete(question);
		System.out.println("Delete Question with id = " + id);
	}
}
